package Try;

/**
 * Created by dell on 2017/5/14.
 */
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.*;
import java.util.Random;

/**
 * Created by 蜡笔小新丶 on 2017/5/12.
 */
public class Practice {
    Random ran = new Random();
    private String sym,Ti="";
    private int num,trues=0;
    private List<String > list,list1,list2;
    public Practice(){
        list = new ArrayList<String>();
        list1 = new ArrayList<String>();
        list2 = new ArrayList<String>();
    }

    public void Ti(int number,int many){
        many = Ticlass(many);
        for (int j=0;j<number;j++) {
            String ti="";
            for (int i = 0; i < many; i++) {
                int A = ran.nextInt(20)+1;
                if (parity(i))
                    ti += getSym() + " ";
                else
                    ti += A + " ";
            }
            Ti = ti;
            list.add(Ti);
        }
    }

    public void bracket() {
        for (int i = 0; i < list.size(); i++) {
            String Ti = list.get(i);
            StringTokenizer token = new StringTokenizer(Ti);

            int B = ran.nextInt(10);
            if(B==0){
                list1.add("(");
            }
            while (token.hasMoreTokens()) {
                String A = token.nextToken();
                if (isOperator(A)) {
                    int num = ran.nextInt(5);
                    list1.add(A);
                    if (num == 0) {
                        list1.add("(");
                        list1.add(token.nextToken());
                    }
                } else {
                    list1.add(A);
                    int num = ran.nextInt(5);
                    if (num == 0 && list1.contains("(") && !list1.contains(")"))
                        if(list1.size()-list1.indexOf("(")<3)
                            break;
                        else
                            list1.add(")");
                }
            }
            if(list1.lastIndexOf("(")==list1.size()-2)
                list1.remove(list1.size()-2);
            if(list1.contains("(") && !list1.contains(")")) {
                if (list1.indexOf("(") == list1.lastIndexOf("(")) {
                    list1.remove(list1.indexOf("("));
                    list1.add(")");
                }else list1.add(")");
            }
            String ti = "";
            for(String j:list1)
                ti += j + " ";
            list1.clear();
            list2.add(ti);
        }
    }

    public void show(){
        Original ori = new Original();
        MyDC coun = new MyDC();
        Scanner scan = new Scanner(System.in);
        ListIterator<String> li = list.listIterator();
        while (li.hasNext()){
            String A = li.next();
            li.remove();
            ori.evaluate(A);
            int result = coun.evaluate(ori.getMessage());

            System.out.print(A + " =");
            int B = scan.nextInt();
            if(result==B){
                System.out.println("正确！");
                trues++;
            }else {
                System.out.printf("错误！");
                System.out.println("正确答案为：" + result);
            }
        }
    }

    public int getTrues(){
        return trues;
    }

    public String getSym(){
        int A = ran.nextInt(4);
        switch (A){
            case 0:
                sym = "+";
                break;
            case 1:
                sym = "-";
                break;
            case 2:
                sym = "*";
                break;
            case 3:
                sym = "/";
                break;
        }
        return sym;
    }

    public boolean parity(int num){
        if(num%2==1)
            return true;
        else
            return false;
    }

    public int Ticlass(int many){
        int A = 1;
        for (int i=0;i<many;i++) {
            A += 2;
        }
        return A;
    }

    private boolean isOperator (String token)
    {
        return ( token.equals("*") || token.equals("/") ||
                token.equals("+") || token.equals("-"));
    }
}
